package com.c.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.druid.pool.DruidDataSource;
import com.c.service.admin.UploadExcelService;

import cn.hutool.core.date.DateTime;
import cn.hutool.db.Entity;
import cn.hutool.db.SqlRunner;

@Controller
@RequestMapping("/excel")
public class UploadExcelController {

	@Autowired
	private UploadExcelService ues;

	@Autowired
	private DruidDataSource ds;

	@RequestMapping(value = "/upload")
	@ResponseBody
	public Map<String, Object> fileUpload(HttpServletRequest request, HttpServletResponse response) throws Exception {
		MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
		Iterator<String> ite = multiRequest.getFileNames();
		while (ite.hasNext()) {
			MultipartFile multifile = multiRequest.getFile(ite.next());
			System.out.println("收到文件：" + multifile.getOriginalFilename());
			Entity log = Entity.create("log").set("create_time", new DateTime(new Date()).toString())
					.set("operation", multifile.getOriginalFilename() + "上传成功").set("operator", "管理员");
			SqlRunner runner = SqlRunner.create(ds);
			runner.insert(log);
			ues.saveData(multifile);
		}
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("code", 0);
		res.put("msg", "");
		res.put("data", "");
		return res;
	}
}
